package com.summer.socket;

//import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
//import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 传统oio实现
 * @author Administrator
 *
 */
public class SimpleCase1 {
	

	public static void main(String[] args) throws IOException {
		boolean isStop = false;
		@SuppressWarnings("resource")
		ServerSocket server = new ServerSocket(9999);
		while(!isStop){
			//会阻塞
			Socket socket = server.accept();
			System.out.println("连接了一个新的客户");
//			handelSocket(socket);
			new Thread(()->handelSocket(socket)).start();
		}
		
	}
	
	public static void handelSocket(Socket socket){
//		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//		String msg;
		//会阻塞
//		while((msg = in.readLine())!=null){
//			System.out.println(msg);
//			break;
//		}
		try {
			byte[] msg = new byte[1024];
			//会阻塞
			InputStream input = socket.getInputStream();
			while(true){
				int len = input.read(msg);
				if(len != -1)
					System.out.println(new String(msg,0,len));
				else
					break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
